package com;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.widget.Button;

import com.Ieasy.Tool.DownImage;
import com.Ieasy.Tool.NetPrinter;
import com.Ieasy.Tool.Tool;
//import com.Ieasy.Tool.ScanCode;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



public class AndroidtoJs extends Object {
    private Button scanBtn;
    //private NetPrinter NetPrinter;
    // 定义JS需要调用的方法
    // 被JS调用的方法必须加入@JavascriptInterface注解
    @JavascriptInterface
    public void hello(String msg) throws Exception {

        /*System.out.println("打印机测试");
        NetPrinter NetPrinter = new NetPrinter();
        boolean isopen = NetPrinter.Open("192.168.0.200", 9100);
        NetPrinter.PrintImg("http://192.168.0.81/static/uploads/picture/20180515/d3d40b8cea6531ce551190b6357ee6ff.png");
        NetPrinter.CutPage(5);
        NetPrinter.Close();*/
    }
    @JavascriptInterface
    public boolean printerTest(String ipaddress, String data) throws JSONException {
        System.out.println("打印机测试");
        String json = data;
        JSONArray jsonArray = new JSONArray(json);
        if (jsonArray.length() > 0) {
            NetPrinter NetPrinter = new NetPrinter();
            boolean isopen = NetPrinter.Open(ipaddress, 9100);
            if (isopen) {
                NetPrinter.Set();
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject job = jsonArray.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                    String title = (String) job.get("title");
                    int nFontAlign = (int) job.get("font_align");
                    int nFontSize = (int) job.get("font_size");
                    int isEnter = (int) job.get("is_enter");
                    NetPrinter.PrintText(title, nFontAlign, nFontSize, 0);
                    if (isEnter > 0) {
                        NetPrinter.PrintNewLines(isEnter);
                    }
                }

                NetPrinter.CutPage(5);

                //
                NetPrinter.Close();
            }
        }
        return true;
    }
    @JavascriptInterface
    public boolean printer(String ipaddress, String pszString, int isCutPage) throws JSONException {
        JSONArray ip = new JSONArray(ipaddress);
        JSONArray jsonArray = new JSONArray(pszString);
        System.out.println(jsonArray);
        System.out.println(ip);

        for (int n = 0; n < ip.length(); n++) {
            JSONObject address = ip.getJSONObject(n);
            if (jsonArray.length() > 0) {
                NetPrinter NetPrinter = new NetPrinter();
                System.out.println(ip);
                String addressIp = (String) address.get("printer_config");
                System.out.println("ip 地址"+addressIp);
                boolean isopen = NetPrinter.Open(addressIp, 9100);
                if (isopen) {
                    NetPrinter.Set();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject job = jsonArray.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                        String title = (String) job.get("title");
                        int nFontAlign = (int) job.get("font_align");
                        int nFontSize = (int) job.get("font_size");
                        int isEnter = (int) job.get("is_enter");
                        NetPrinter.PrintText(title, nFontAlign, nFontSize, 0);
                        if (isEnter > 0) {
                            NetPrinter.PrintNewLines(isEnter);
                        }
                    }

                    if (isCutPage == 1) {
                        NetPrinter.CutPage(5);
                    } else {
                        NetPrinter.PrintNewLines(5);
                    }
                    //
                    NetPrinter.Close();
                }
            }
        }
        return true;
    }

    @JavascriptInterface
    public void scanCode() throws JSONException {
        System.out.println("开始扫描");
        /*Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
        *//*ZxingConfig是配置类
         *可以设置是否显示底部布局，闪光灯，相册，
         * 是否播放提示音  震动
         * 设置扫描框颜色等
         * 也可以不传这个参数
         * */


    }

}

